﻿using Microsoft.Win32;

namespace TroubleshootingofBeanBagAssistant
{
	public class SystemAagent
	{
		// 判断系统代理是否异常
		public static bool IsSystemProxyAbnormal(out string proxyInfo)
		{
			proxyInfo = "";
			try
			{
				using var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", false);
				if (key != null)
				{
					object enable = key.GetValue("ProxyEnable");
					object server = key.GetValue("ProxyServer");
					proxyInfo = server?.ToString() ?? "";

					if (enable is int proxyEnable && proxyEnable == 1)
					{
						proxyInfo = server?.ToString() ?? "";
						// 这里可以根据你的业务判断什么是“异常”，比如只要开启了代理就算异常
						return true;
					}
				}
			}
			catch { }
			return false;
		}

		// 恢复系统代理为“未启用”
		public static void RestoreSystemProxy()
		{
			try
			{
				using var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
				if (key != null)
				{
					key.SetValue("ProxyEnable", 0, RegistryValueKind.DWord);
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine($"恢复代理失败: {ex.Message}");
			}
		}
	}
}
